package com.xinongbest.dao.impl;

import com.xinongbest.dao.UserDao;
import com.xinongbest.entity.User;
import com.xinongbest.utils.ConnectionUtils;
import com.xinongbest.utils.DruidUtils;
import com.xinongbest.utils.TransactionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lemonwan
 * 获取用户信息
 */
@SuppressWarnings("all")
public class UserDaoImpl implements UserDao {
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    /**
     * 该方法是最原始的版本
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<User> selectAllUser() throws SQLException {
        List<User> users = new ArrayList<>();
//        Connection connect = DruidUtils.getInstance().getConnection();
//        String sql = "select * from sys_user";
//        PreparedStatement statement = connect.prepareStatement(sql);
//        ResultSet resultSet = statement.executeQuery();
//        User user;
//        while (resultSet.next()) {
//            user = new User();
//            user.setId(resultSet.getLong("user_id"));
//            user.setUsername(resultSet.getString("username"));
//            user.setPassword(resultSet.getString("password"));
//            users.add(user);
//        }
        return users;
    }

    /**
     * 该方法是第二个版本
     *
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public int updateUserById(User user) throws SQLException {
        int res = 0;
//        try {
//            TransactionManager.getInstance().startTransaction();
//            /** 使用链接管理工具 */
//            // Connection connect = DruidUtils.getInstance().getConnection();
//            Connection connect = ConnectionUtils.getInstance().getConnect();
//            String sql = "update sys_user set username = ? where user_id = ?";
//            PreparedStatement statement = connect.prepareStatement(sql);
//            statement.setString(1, user.getUsername());
//            statement.setLong(2, user.getId());
//            statement.executeUpdate();
//            // 手动制造异常，检验事务提交
//            // int i = 1 / 0;
//            sql = "update sys_role_user set role_id = ? where user_id = ?";
//            PreparedStatement preparedStatement = connect.prepareStatement(sql);
//            preparedStatement.setLong(1, user.getRoleId());
//            preparedStatement.setLong(2, user.getId());
//            res = preparedStatement.executeUpdate();
//            TransactionManager.getInstance().commit();
//        } catch (Exception e) {
//            TransactionManager.getInstance().rollback();
//        }
        return res;
    }

    /**
     * 该方法是第三个版本：使用动态代理抽取事务控制逻辑
     *
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public Integer updateUserByIdByDynamicTrans(User user) throws SQLException {
        int res = 0;
//        Connection connect = ConnectionUtils.getInstance().getConnect();
//        String sql = "update sys_user set username = ? where user_id = ?";
//        PreparedStatement statement = connect.prepareStatement(sql);
//        statement.setString(1, user.getUsername());
//        statement.setLong(2, user.getId());
//        statement.executeUpdate();
//        // 手动制造异常，检验事务提交
//        int i = 1 / 0;
//        sql = "update sys_role_user set role_id = ? where user_id = ?";
//        PreparedStatement preparedStatement = connect.prepareStatement(sql);
//        preparedStatement.setLong(1, user.getRoleId());
//        preparedStatement.setLong(2, user.getId());
//        res = preparedStatement.executeUpdate();
//        preparedStatement.close();
        return res;
    }

    /**
     * 该方法是最终版本：使用xml配置管理容器依赖
     *
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public Integer updateUserByIdByDynamicTransNew(User user) throws SQLException {
        Connection connect = connectionUtils.getConnect();
        String sql = "update sys_user set username = ? where user_id = ?";
        PreparedStatement statement = connect.prepareStatement(sql);
        statement.setString(1, user.getUsername());
        statement.setLong(2, user.getId());
        statement.executeUpdate();
        // 手动制造异常，检验事务提交
        // int i = 1 / 0;
        sql = "update sys_role_user set role_id = ? where user_id = ?";
        PreparedStatement preparedStatement = connect.prepareStatement(sql);
        preparedStatement.setLong(1, user.getRoleId());
        preparedStatement.setLong(2, user.getId());
        int res = preparedStatement.executeUpdate();
        preparedStatement.close();
        return res;
    }
}
